package com.seed.service.sysmanager;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.seed.commons.dbquery.PageModelForSQL;
import com.seed.commons.dbutil.DB;
import com.seed.commons.dbutil.DBBean;
import com.seed.commons.util.RandomUtil;
import com.seed.web.form.sysmanager.InsideUserForm;

@Service
public class InsideService implements IInsideUser{
	
	private Log log = LogFactory.getLog(getClass()) ;
	@Override
	public boolean setUserRole(String userid, String[] roleids) {
		System.out.println("-"+userid);
		String sql = "INSERT INTO SEED_USER_ROLE(URID,USERID,ROLEID) VALUES(?,?,?)" ;
		for (int i = 0; i < roleids.length; i++) {
			try {
				System.out.println(roleids[i] + "=="+userid);
				DB.insert(sql, new Object[]{RandomUtil.getRandomNum(5),userid,roleids[i]}) ;
			} catch (Exception e) {
				log.info("为用户添加角色权限错误：" + e.getMessage()) ;
				e.printStackTrace();
				return false ;
			}
		}
		return true;
	}
	@Override
	public boolean saveInsideUser(InsideUserForm ins) {
		
		String sql = "INSERT INTO SEED_INSIDE_USER(UserId,UserName,EmpNoFK) VALUES(?,?,?)" ;
		
		try {
			DB.insert(sql, new Object[]{RandomUtil.getRandomNum(5),ins.getUserName(),ins.getEmpNoFK()}) ;
			return true ;
		} catch (Exception e) {
			e.printStackTrace();
			log.info("数据插入出错：" + e.getMessage());
			log.info("执行SQL：" + sql);
		}
		return false;
	}

	@Override
	public void deleteInsideUser(String id) {
		
		
	}

	@Override
	public void updateInsideUser(InsideUserForm ins) {
		
		
	}

	@Override
	public DBBean queryInsideUserById(String id) {
		
		
		
		return null;
	}

	@Override
	public PageModelForSQL getScrollData(int currentPage, int lineSize) {
		String sql = "SELECT * FROM " +
				 "(" +
					 "SELECT " +
					 "ROWNUM rn,t1.* " +
					 "FROM (SELECT * from SEED_INSIDE_USER) t1 " +
					 "WHERE ROWNUM <= ?" +
				 ") " +
				 "WHERE rn > ?" ;
		String count = "SELECT COUNT(*) c FROM SEED_INSIDE_USER";
		System.out.println(sql);
		try {
			PageModelForSQL pm = new PageModelForSQL();
			pm.setResultList(DB.query(sql,
					new Object[] { lineSize, currentPage }));
			pm.setTotalRecord(Long.parseLong(DB.getBeanFirst(count, null)
					.getbean("c").toString()));
System.out.println(pm);
			return pm;
		} catch (Exception e) {
			log.info("分页查询出错：" + e.getMessage());
		}
		return null;
	}

}
